package com.zhengxing.jienci.user.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import com.zhengxing.jienci.common.constant.RedisKey;
import com.zhengxing.jienci.common.pojo.SocialUser;
import com.zhengxing.jienci.common.pojo.SysUser;
import com.zhengxing.jienci.common.support.SimpleResponse;
import com.zhengxing.jienci.common.util.CommonUtils;
import com.zhengxing.jienci.user.domain.RankPoint;
import com.zhengxing.jienci.user.mapper.PointDetailMapper;
import com.zhengxing.jienci.user.mapper.RankPointMapper;
import com.zhengxing.jienci.user.mapper.SocailUserMapper;
import com.zhengxing.jienci.user.mapper.UserMapper;
import com.zhengxing.jienci.user.service.UserService;
import com.zhengxing.jienci.web.context.SecurityContext;

@Service
public class UserServiceImpl implements UserService {
	
	@Autowired
	private SocailUserMapper socailUserMapper;
	
	@Autowired
	private UserMapper userMapper;
	
	@Autowired
	private PointDetailMapper pdMapper;
	
	@Autowired
	private RankPointMapper rpMapper;
	
	@Autowired
	private RedisTemplate<String, String> redisTemplate;
	
	@Override
	public SimpleResponse userbinding(SocialUser socialUser){
		String userId = SecurityContext.getUserId();
		socialUser.setUserId(userId);
		int	result = socailUserMapper.insert(socialUser);
		return CommonUtils.msg(result);
	}

	@Override
	public SimpleResponse follow(String followId) {
		String userId = SecurityContext.getUserId();
		//我关注的
		redisTemplate.opsForSet().add(RedisKey.FOLLOW_OPERATION_PREFIX + userId, followId);
		//被关注人的粉丝
		redisTemplate.opsForSet().add(RedisKey.FOLLOWER_OPERATION_PREFIX + followId, userId);
		return CommonUtils.msg(1);
	}

	@Override
	public SysUser getDetailUserById(String userId) {
		SysUser userDetail = userMapper.selectById(userId);
		//获取粉丝数
		Long followers = redisTemplate.opsForSet().size(RedisKey.FOLLOWER_OPERATION_PREFIX + userId);
		userDetail.setFollowerNumber(followers);
		//获取关注数
		Long follows = redisTemplate.opsForSet().size(RedisKey.FOLLOW_OPERATION_PREFIX + userId);
		userDetail.setFollowNumber(follows);
		//获取用户可用积分数
		Integer point = pdMapper.seletPointByUserId(userId);
		point = point==null?0:point;
		userDetail.setAvailableIntegral(point.longValue());
		//获取用户等级
		RankPoint rankPoint = rpMapper.selectRankByPoint(userDetail.getIntegral());
		Integer rank = rankPoint.getRank();
		rank = rank==null?0:rank;
		userDetail.setRank(rank);
		userDetail.setMaxIntegral(rankPoint.getMaxPoint());
		return userDetail;
	}


	@Override
	public SysUser getSimpleUserById(String userId) {
		SysUser userDetail = userMapper.selectById(0);
		return userDetail;
	}

	@Override
	public SimpleResponse updateUser(SysUser sysUsers) {
		int result = userMapper.updateById(sysUsers);
		return CommonUtils.msg(result);
	}

	@Override
	public SimpleResponse isAttention(String followId) {
		String userId = SecurityContext.getUserId();
		Boolean isAttention = redisTemplate.opsForSet().isMember(RedisKey.FOLLOWER_OPERATION_PREFIX + followId, userId);
		return SimpleResponse.success().put("isAttention", isAttention);
	}

	
}
